package com.mytijian.code.service;

import com.mytijian.code.dao.MerchantOrderInfoDAO;
import com.mytijian.code.dao.dataobject.MerchantOrderInfoDO;
import com.mytijian.code.enums.MerchantOrderStatusEnum;
import com.mytijian.code.model.MerchantOrder;
import com.mytijian.utils.BeanUtil;
import com.mytijian.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by shjh on 2017/9/26.
 */
@Service
public class MerchantOrderServiceImpl implements MerchantOrderService {
	
    @Autowired
    private MerchantOrderInfoDAO merchantOrderInfoDAO;

    @Override
    public List<MerchantOrder> listNeedSendCodeOrders() {
        List<MerchantOrderInfoDO> merchantOrderInfoDOs = merchantOrderInfoDAO.selectNeedSendCodeOrders(getBeforeTime());
        if (CollectionUtils.isEmpty(merchantOrderInfoDOs)) {
            return null;
        }
        return BeanUtil.copyBeanList(merchantOrderInfoDOs, MerchantOrder.class);
    }

    private String getBeforeTime() {
        long time = 10 * 60 * 1000;//10分钟
        Date beforeDate = new Date(new Date().getTime() - time);//10分钟前的时间
        return DateUtils.format(DateUtils.YYYY_MM_DD_HMS, beforeDate);
    }

    @Override
    public MerchantOrder getOrderByOuterId(String outerId) {
        MerchantOrderInfoDO orderDO = merchantOrderInfoDAO.selectByOuterId(outerId);
        return BeanUtil.createObject(orderDO, MerchantOrder.class);
    }

    @Override
    public void saveOrder(MerchantOrder tmOrder) {
        MerchantOrderInfoDO tmOrderDO = BeanUtil.createObject(tmOrder, MerchantOrderInfoDO.class);
        merchantOrderInfoDAO.insert(tmOrderDO);
        tmOrder.setId(tmOrderDO.getId());
    }

    @Override
    public MerchantOrder selectByOuterId(String outerId) {
        MerchantOrderInfoDO tmOrderDOExist = merchantOrderInfoDAO.selectByOuterId(outerId);
        return BeanUtil.createObject(tmOrderDOExist, MerchantOrder.class);
    }

    @Override
    public int updateTmOrder(MerchantOrder tmOrder, Integer oldStatus) {
        return merchantOrderInfoDAO.updateTmOrder(BeanUtil.createObject(tmOrder, MerchantOrderInfoDO.class), oldStatus);
    }

	@Override
	public int orderSendCodeNotifySuccess(Integer orderId) {
		MerchantOrderInfoDO orderDO = new MerchantOrderInfoDO();
		orderDO.setId(orderId);
		orderDO.setSendCodeIsSuccess(true);
		return  merchantOrderInfoDAO.updateTmOrder(orderDO, null);
	}
	
	@Override
	public int orderSendCodeNotifyReset(Integer orderId) {
		MerchantOrderInfoDO orderDO = new MerchantOrderInfoDO();
		orderDO.setId(orderId);
		orderDO.setSendCodeIsSuccess(false);
		return  merchantOrderInfoDAO.updateTmOrder(orderDO, null);
	}

    @Override
    public void invalidOrder(Integer orderId, String refundFee, Integer cancelNum) {
        MerchantOrderInfoDO merchantOrder = new MerchantOrderInfoDO();
        merchantOrder.setId(orderId);
        merchantOrder.setStatus(MerchantOrderStatusEnum.UNNORMAL.getCode());
        merchantOrder.setRefundFee(refundFee);
        if (cancelNum != null) {
            merchantOrder.setCancelNum(cancelNum);
        }
        merchantOrderInfoDAO.updateTmOrder(merchantOrder, null);
    }
}
